﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.Equipment
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class SwitchBoxesLabel : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result = SwitchView.SetUsableView((HYView.CmdViewType)17, cmdData);
			if (result != Result.Succeeded)
			{
				return result;
			}
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_doc = this.m_uiDoc.Document;
			this.m_form = new SwitchBoxesLabelForm();
			this.m_form.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			Result result2;
			try
			{
				for (;;)
				{
					this.m_refStart = this.m_uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElectricSwitchBoxesFilter(), "请选择起始配电柜");
					for (;;)
					{
						this.m_refEnd = this.m_uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new ElectricSwitchBoxesFilter(), "请选择末端配电柜");
						if (!(this.m_refEnd.ElementId == this.m_refStart.ElementId))
						{
							break;
						}
						YJKMessageBox.Show("两次配电柜选择是同一个实体,无法完成操作,请重新选择!");
					}
					FamilyInstance familyInstance = this.m_doc.GetElement(this.m_refStart) as FamilyInstance;
					if (familyInstance != null)
					{
						FamilyInstance familyInstance2 = this.m_doc.GetElement(this.m_refEnd) as FamilyInstance;
						if (familyInstance2 != null)
						{
							SwitchBoxesReLayout.EquipInfo item = new SwitchBoxesReLayout.EquipInfo(familyInstance2);
							List<SwitchBoxesReLayout.EquipInfo> adjoinEquipsGroup = SwitchBoxesReLayout.GetAdjoinEquipsGroup(familyInstance, this.m_doc);
							if (adjoinEquipsGroup.Count > 1 && adjoinEquipsGroup.IndexOf(item) > -1)
							{
								SwitchBoxesReLayout.EquipInfo item2 = new SwitchBoxesReLayout.EquipInfo(familyInstance);
								int num = adjoinEquipsGroup.IndexOf(item2);
								int num2 = adjoinEquipsGroup.IndexOf(item);
								int num3 = (num < num2) ? 1 : -1;
								Transaction transaction = new Transaction(this.m_doc, "ModifyLabelIdx");
								try
								{
									transaction.Start();
									int num4 = this.m_form.StartNum;
									int num5 = this.m_form.SortAsc ? 1 : -1;
									for (int num6 = num; num6 != num2 + num3; num6 += num3)
									{
										string text = this.m_form.Prefix;
										text += num4;
										SwitchBoxesReLayout.EquipInfo equipInfo = adjoinEquipsGroup[num6];
										equipInfo.LabelIdx = num4;
										(this.m_doc.GetElement(equipInfo.ElemID) as FamilyInstance).GetParameter(BuiltInParameter.RBS_ELEC_PANEL_NAME).Set(text);
										num4 += num5;
									}
									transaction.Commit();
									continue;
								}
								catch
								{
									transaction.RollBack();
									continue;
								}
							}
						}
					}
					YJKMessageBox.Show("两次电气柜选择不属于同一组,无法完成操作,请重新选择起始配电柜!");
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				this.m_form.Close();
				result2 = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				this.m_form.Close();
				result2 = Autodesk.Revit.UI.Result.Failed;
			}
			return result2;
		}

		private SwitchBoxesLabelForm m_form;

		private new UIDocument m_uiDoc;

		private new Document m_doc;

		private Reference m_refStart;

		private Reference m_refEnd;
	}
}
